﻿using System.Linq;
using LudeonTK;
using RimWorld;
using UnityEngine;
using Verse;
using static UnityEngine.GraphicsBuffer;

namespace ARMIR
{
    public class ShieldGenerator {
        public static Pawn GenerateShield(Faction faction) {
            //创建pawn请求
            PawnGenerationRequest request = new PawnGenerationRequest(
                kind: PawnKindDefOf.Colonist, // 使用殖民者类型
                faction: faction, // 设置阵营
                context: PawnGenerationContext.NonPlayer, // 生成上下文
                fixedGender: Gender.Male // 固定性别为男性
            );

            // 生成pawn
            Pawn pawn = PawnGenerator.GeneratePawn(request);

            //穿戴
            pawn.apparel?.DestroyAll(); //清理原有衣物
            //盾卫头盔
            ThingDef helmetDef = DefDatabase<ThingDef>.GetNamed("ARMIR_shieldHelmet");
            if (helmetDef != null) {
                Thing helmet = ThingMaker.MakeThing(helmetDef);
                pawn.apparel.Wear((Apparel)helmet); // 强制穿戴
            }
            //盾卫护甲
            ThingDef CoatDef = DefDatabase<ThingDef>.GetNamed("ARMIR_shieldArmor"); 
            if (CoatDef != null) {
                Thing Coat = ThingMaker.MakeThing(CoatDef);
                pawn.apparel.Wear((Apparel)Coat); // 强制穿戴
            }
            //盾卫护盾
            ThingDef ShieldDef = DefDatabase<ThingDef>.GetNamed("ARMIR_listShield");
            if (ShieldDef != null)
            {
                Apparel listShield = ThingMaker.MakeThing(ShieldDef) as Apparel;
                pawn.apparel.Wear(listShield, dropReplacedApparel: false, locked: true);// 强制穿戴不可取下
            }

            //设置体型
            pawn.story.bodyType = BodyTypeDefOf.Male; //体型

            // 设置特性
            pawn.story.traits.allTraits.Clear();
            pawn.story.traits.GainTrait(new Trait(TraitDefOf.Brawler)); // 格斗者

            // 设置技能
            pawn.skills.GetSkill(SkillDefOf.Melee).Level = 10 + Random.Range(-2, 8); //增加近战能力
            pawn.skills.GetSkill(SkillDefOf.Melee).passion = Passion.Major;

            // 添加装备
            pawn.equipment.DestroyAllEquipment();
            Thing weapon = ThingMaker.MakeThing(ThingDef.Named("ARMIR_infectersShield"));
            pawn.equipment.AddEquipment(weapon as ThingWithComps);

            return pawn;
        }

        // 调用示例（可以在命令或事件中调用）
        [DebugAction("Rosmontis In Rimworld", "Spawn Shield", actionType = DebugActionType.Action)]
        public static void SpawnARMIRShield() {
            Pawn ros = GenerateShield(Faction.OfPlayer);
            GenSpawn.Spawn(ros, UI.MouseCell(), Find.CurrentMap);
            Messages.Message("Infecters Shield has arrived!", MessageTypeDefOf.PositiveEvent);
        }
    }
}
